/*
 * SteepTimer - A timer designed for tea fanatics
 * Copyright (C) 2011  Wesley Crossman
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "GrowingTextEdit.h"
#include <QScrollArea>
#include <QDebug>
#include <QPaintEvent>
#include <QPainter>
#include <cmath>

GrowingTextEdit::GrowingTextEdit(QWidget *parent, int sizeLimit) :
            QTextEdit(parent),
            m_fittedWidth(0),
            m_fittedHeight(0),
            m_fittedOrientations(0),
            m_sizeLimit(sizeLimit) {
    connect( this, SIGNAL(textChanged()), SLOT(textChangeMade()) );
    fitToDocument(Qt::Vertical);
}

QSize GrowingTextEdit::sizeHint() const {
    QSize sizeHint = QTextEdit::sizeHint();
    if(m_fittedOrientations & Qt::Horizontal)
        sizeHint.setWidth(m_fittedWidth < m_sizeLimit ? m_fittedWidth : m_sizeLimit);
    if(m_fittedOrientations & Qt::Vertical)
        sizeHint.setHeight(m_fittedHeight < m_sizeLimit ? m_fittedHeight : m_sizeLimit);
    return sizeHint;
}

//Written using a snippet by Ivan D Vasin
//http://stackoverflow.com/questions/1153714/pyqt-how-to-handle-auto-resize-of-widgets-when-their-content-changes
void GrowingTextEdit::fitToDocument(Qt::Orientations orientations) {
    document()->setTextWidth(viewport()->width());
    QSize documentSize(document()->size().toSize());
    QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    if(orientations & Qt::Horizontal) {
        m_fittedWidth = documentSize.width() + (width() - viewport()->width());
        sizePolicy.setHorizontalPolicy(QSizePolicy::Fixed);
    }
    if(orientations & Qt::Vertical) {
        m_fittedHeight = documentSize.height() + (height() - viewport()->height());
        sizePolicy.setVerticalPolicy(QSizePolicy::Fixed);
        setVerticalScrollBarPolicy(
                    m_fittedHeight < m_sizeLimit ? Qt::ScrollBarAlwaysOff : Qt::ScrollBarAsNeeded);
    }
    m_fittedOrientations = orientations;
    setSizePolicy(sizePolicy);
    updateGeometry();
}

void GrowingTextEdit::paintEvent(QPaintEvent *event) {
    QTextEdit::paintEvent(event);
    QPainter painter(viewport());
    painter.setPen(QColor(60, 60, 80));
    if (toPlainText().size() == 0 && !viewport()->hasFocus()) {
        painter.drawText(rect().adjusted(3,0,0,-7),
                         tr("Type your reminder message here."),
                         QTextOption(Qt::AlignLeft|Qt::AlignVCenter));
    }
}

void GrowingTextEdit::focusInEvent(QFocusEvent *e) {
    viewport()->update();
    QTextEdit::focusInEvent(e);
}

void GrowingTextEdit::focusOutEvent(QFocusEvent *e) {
    viewport()->update();
    QTextEdit::focusOutEvent(e);
}

void GrowingTextEdit::textChangeMade() {
    fitToDocument(Qt::Vertical);
}
